
<template>
  <views class="invoice_log">
    <viewTitle :title="'开票记录'" :border="false"></viewTitle>
    <!-- 搜索栏  企业端-代理商端-服务商端 -->
    <div class="search flex flexC">
      <!-- 批次编号-所属服务商-发票抬头-开票状态-申请时间 -->
      <el-form ref="form" label-width="auto" :inline="true">
        <div class="search_input flex flexR center">
          <div class="bank_statement flex flexR center">
            <el-form-item label="发票抬头:">
              <el-input v-model="form.enterprise_name" placeholder="请输入发票抬头" :clearable="true"></el-input>
            </el-form-item>
          </div>
          <div class="pay_state flex flexR center">
            <el-form-item label="开票状态:">
              <el-select v-model="form.invoice_status" clearable placeholder="请选择">
                <el-option
                  v-for="item in status_list"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </div>
        </div>
      </el-form>
      <!-- 充值时间  按钮 (查询、重置、导出列表、录单)-->
      <el-form ref="form" label-width="auto" :inline="true">
        <div class="pay_btn flex">
          <!-- 充值时间 -->
          <div class="pay_time flex flexR center">
            <el-form-item label="申请时间:">
              <el-date-picker
                v-model="apply_time"
                type="datetimerange"
                align="right"
                :default-time="['0:00:00','23:59:59']"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions"
                @change="changeTime"
              ></el-date-picker>
            </el-form-item>
          </div>
          <!-- 按钮（查询、重置、导出列表、录单）  录单属于服务商端 -->
          <el-form-item>
            <div class="flex flexR pay_btn_list">
              <el-button type="primary" @click="getData" icon="el-icon-search"  >查看</el-button>
              <el-button @click="reset()" icon="el-icon-refresh">重置</el-button>
              <el-button type="danger" @click="exportExcel" icon="el-icon-download">导出列表</el-button>
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <div class="search_input flex flexR center">
      <div :style="{marginLeft:'15px'}" class="show-title-money flex flexR">
        <div class="flex flexR">
          <div>交易笔数</div>
          <div class="price">{{pagination.total_count}}</div>
          <div>笔</div>
        </div>
        <div class="flex flexR">
          <div>交易金额(元)</div>
          <div class="price">￥{{pagination.summy ? $splitK(pagination.summy) : ''}}</div>
        </div>
      </div>
    </div>

    <!-- 展示列表 -->
    <div class="table_list">
      <el-table
        border
        :data="tableData"
        style="width: 100%;max-width:none;"
        :header-cell-style="getRowClass"
        :select-on-indeterminate="true"
        :fit="true"
        :lazy="true"
        @row-click="clickRow"
        max-height="450"
      >
        <el-table-column fixed type="index" label="序号" width="50" ></el-table-column>
        <el-table-column fixed prop="create_time" label="申请时间"></el-table-column>
        <el-table-column prop="facilitator_sn" label="服务商编码" ></el-table-column>
        <el-table-column prop="facilitator_name" label="服务商名称" ></el-table-column>
        <el-table-column prop="enterprise_sn" label="企业编码" ></el-table-column>
        <el-table-column prop="enterprise_name" label="企业名称" ></el-table-column>
        <el-table-column prop="money" label="发票金额" ></el-table-column>
        <el-table-column prop="invoice_type" label="开票税目" ></el-table-column>
        <!--<el-table-column prop="receiver_address" label="收货地址" width="auto"></el-table-column>-->
        <el-table-column prop="invoice_status" label="开票状态"></el-table-column>
        <el-table-column prop="open_upload_img" label="履约确认函" >
          <template slot-scope="scope">
            <el-image
              style="width: 50px; height: 50px"
              :src="scope.row.apply_img"
              :preview-src-list="[scope.row.apply_img]"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" width="120"></el-table-column>
        <el-table-column prop="zip" label="操作" fixed="right" width="200" >
          <template slot-scope="scope">
            <div class="table_operation">
              <el-button
                type="danger"
                v-if="$store.state.terrace==3 && isCheck"
                @click="modifyShow(scope)"
              >修改</el-button>
              <el-button type="primary" @click="toDetail(scope)" v-if="isLook" >查看</el-button>
              <el-button
                type="danger"
                v-if="$store.state.terrace==1 && scope.row.status == 0"
                @click="cancelInvoice(scope.row.enterprise_invoice_id)"
              >取消</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="flex flexR paging">
      <el-pagination
        style="background:#fff; padding:10px 0"
        background
        layout="total, sizes,prev, pager, next, jumper"
        @size-change="(page_size) => {this.pagination.page_size = page_size;this.getData()}"
        @current-change="(page_size) => {this.pagination.page_start = page_size;this.getData()}"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pagination.page_size"
        :total="pagination.total_count"
        :current-page="pagination.current_page"
        :page-count="pagination.page_count"
      ></el-pagination>
    </div>

    <!--  修改开票 -->
    <div class="modi_invoice">
      <modelBox :show="modi_invoice_show" v-if="$store.state.terrace==3">
        >
        <div class="add_lists">
          <div class="flex flexR add_lists_title">
            <viewTitle :title="'修改开票信息'" :border="false"></viewTitle>
            <div class="el-icon-close cp" @click="close_Model"></div>
          </div>
          <div class="add_input_lists">
            <div class="flex flexR center add_inputs">
              <div class="add_input_lists_title">发票抬头:</div>
              <div class="add_input_lists_item">
                <div>{{row.enterprise_name}}</div>
              </div>
            </div>
          </div>
          <div class="add_input_lists">
            <div class="flex flexR center add_inputs">
              <div class="add_input_lists_title">发票金额:</div>
              <div class="add_input_lists_item">
                <div>￥{{row.money}}</div>
              </div>
            </div>
          </div>

          <div class="add_input_lists">
            <div class="flex flexR center add_inputs">
              <div class="add_input_lists_title">税务登记号:</div>
              <div class="add_input_lists_item">
                <div>{{row.enterprise_code}}</div>
              </div>
            </div>
          </div>

          <div class="add_input_lists">
            <div class="flex flexR center add_inputs">
              <div class="add_input_lists_title">注册电话:</div>
              <div class="add_input_lists_item">
                <div>{{row.receiver_mobile}}</div>
              </div>
            </div>
          </div>

          <div class="add_input_lists">
            <div class="flex flexR center add_inputs">
              <div class="add_input_lists_title">注册地址:</div>
              <div class="add_input_lists_item">
                <div>{{row.receiver_address}}</div>
              </div>
            </div>
          </div>

          <div class="add_input_lists">
            <div class="flex flexR center add_inputs">
              <div class="add_input_lists_title">开户银行:</div>
              <div class="add_input_lists_item">
                <div>{{row.bank_name}}</div>
              </div>
            </div>
          </div>

          <div class="add_input_lists">
            <div class="flex flexR center add_inputs">
              <div class="add_input_lists_title">银行账号:</div>
              <div class="add_input_lists_item">
                <div>{{row.bank_sn}}</div>
              </div>
            </div>
          </div>

          <div class="add_input_lists">
            <div class="flex flexR center add_inputs">
              <div class="add_input_lists_title">发票状态:</div>
              <div class="add_input_lists_item">
                <el-select v-model="select_status" clearable placeholder="请选择">
                  <el-option
                    v-for="(item,index) in row.status_list"
                    :key="index"
                    :label="item"
                    :value="parseInt(index)"
                  ></el-option>
                </el-select>
              </div>
            </div>
          </div>
          <!-- 确认 取消 -->
          <div class="Confirm_cancel flex flexR center">
            <el-button type="primary" @click="modifyInvoice(row.enterprise_invoice_id)">修改</el-button>
            <el-button type @click="close_Model">取消</el-button>
          </div>
        </div>
      </modelBox>
    </div>
  </views>
</template>



<script>
import {mapGetters} from 'vuex'

export default {
  data() {
    return {
      download_url: "",
      modi_invoice_show: false,
      pagination: {
        page_start: 0, //搜索开始页码
        current_page: 0, //当前页码
        page_count: 0, //总页数
        page_size: 10, //每页数量
        total_count: 0 //总数量
      },
      select_status: 0,
      row: {},
      //  三段综合  企业端  代理商端  服务商端
      //0：已申请、1：已通过待开票、2：已开票、3：已邮寄、4：审核未过、5：已取消、6：已作废（表示已红冲）
      form: {
        enterprise_sn: "", //    发票抬头
        facilitator_key: "", //    银行流水
        facilitator_id: 0, //    充值状态
        invoice_status: "", //  开票状态列表
        enterprise_invoice_address_id: "", //    申请时间
        enterprise_bank_id: "", //    申请时间
        created_time_start: "", //    申请时间
        created_time_end: "", //    申请时间
        pay_time_start: "", //    申请时间
        pay_time_end: "" //    申请时间
      },
      tableData: [],
      apply_time: "",
      status_list: [
        { value: "", label: "全部" },
        { value: 0, label: "已申请" },
        { value: 1, label: "已通过待开票" },
        { value: 2, label: "已开票" },
        { value: 3, label: "已邮寄" },
        { value: 4, label: "审核未过" },
        { value: 5, label: "已取消" }
        // { value: 6, label: "已作废" }
      ],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      }
    };
  },
  computed:{
    ...mapGetters(['getButtonPermission']),
    isLook(){
      return  this.getButtonPermission[this.$route.path]  &&  this.getButtonPermission[this.$route.path].look ? 1 : 0;
    },
    isCheck(){
      return  this.getButtonPermission[this.$route.path]  &&  this.getButtonPermission[this.$route.path].check ? 1 : 0;
    }

  },
  mounted() {
    this.getData();
  },
  methods: {
    //    修改第一行样式
    getRowClass({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return "background: #FAFAFA ";
      } else {
        return "";
      }
    },
    //     点击单行
    clickRow(row, column, event) {
      console.log(row, column, event);
    },
    //    修改是否换行
    changeStyleRow(width) {
      let _width = width - 290 - 350 - 300 - 50 - 25;
      if (_width > 300) {
        this.marginLeft = 25;
      } else {
        this.marginLeft = 0;
      }
    },
    // 开票记录-查看详情
    toDetail(scope) {
      this.$toRoutePath({
        path: "/invoice_log/invoice_look",
        edit: true,
        query: { enterprise_invoice_id: scope.row.enterprise_invoice_id }
      });
    },
    getData() {
      this.form.pagination = this.pagination;
      if (this.$store.state.terrace == 1) {
        this.getEnterpriseData();
      }
      if (this.$store.state.terrace == 3) {
        this.getFacilitatorData();
      }
    },
    //获取数据
    getEnterpriseData() {
      let { code, msg, data } = this.$api.sendSync(
        this.$mapapi.egetInvoiceList,
        this.form
      );
      if (code != 200 && code != 501) {
        this.$message.error(msg);
        return;
      }
      this.tableData = data.list;
      this.download_url = data.download_url;
      this.pagination = data.pagination;
    },
    getFacilitatorData() {
      let { code, msg, data } = this.$api.sendSync(
        this.$mapapi.finvoiceList,
        this.form
      );
      if (code != 200 && code != 501) {
        this.$message.error(msg);
        return;
      }
      this.tableData = data.list;
      this.download_url = data.download_url;
      this.pagination = data.pagination;
    },
    changeTime() {
      if (this.apply_time) {
        this.form.created_time_start = this.apply_time[0]/1000;
        this.form.created_time_end = this.apply_time[1]/1000;
      } else {
        this.form.created_time_start = "";
        this.form.created_time_end = "";
      }
    },
    exportExcel() {
      this.$import_excal(this.download_url || "");
    },
    close_Model() {
      this.modi_invoice_show = false;
    },
    modifyShow(scope) {
      this.modi_invoice_show = true;
      let { code, msg, data } = this.$api.sendSync(
        this.$mapapi.finvoiceDetail,
        {
          enterprise_invoice_id: scope.row.enterprise_invoice_id
        }
      );
      if (code != 200 && code != 501) {
        this.$message.error(msg);
        return;
      }
      this.row = data;
      this.select_status = data.status;
      console.log("data", data);
    },
    cancelInvoice(enterprise_invoice_id) {
      this.$confirm("你确定要向取消申请吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let { code, msg } = this.$api.sendSync(this.$mapapi.ecancelInvoice, {
            enterprise_invoice_id: enterprise_invoice_id
          });
          if (code != 200) {
            this.$message.error(msg);
            return;
          }
          this.getData();
          this.$message({
            type: "success",
            message: "已更新!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    modifyInvoice(enterprise_invoice_id) {
      let { code, msg } = this.$api.sendSync(this.$mapapi.fmodifyInvoice, {
        enterprise_invoice_id: enterprise_invoice_id,
        status: this.select_status
      });
      if (code != 200 && code != 501) {
        this.$message.error(msg);
      } else {
        this.$message.success(msg);
      }
      this.getData();
      this.close_Model();
    },
    showImg(url) {
      this.$previewImg({ src: url || "" });
    },
    // 重置
    reset() {
      this.form = {
        enterprise_sn: "", //    发票抬头
        facilitator_key: "", //    银行流水
        facilitator_id: 0, //    充值状态
        invoice_status: "", //  开票状态列表
        enterprise_invoice_address_id: "", //    申请时间
        enterprise_bank_id: "", //    申请时间
        created_time_start: "", //    申请时间
        created_time_end: "" //    申请时间
      };
      this.apply_time = [];
      this.getData();
    }
  },
  created() {}
};
</script>


<style lang="less" :scope="true"  src="@/style/invoice/invoice_log.less" >
</style>